<?php

namespace App\Utility;

class MTool
{

    public static function var_dump($data = [])
    {
        ob_start();
        var_export($data);
        $content = ob_get_clean();
        return $content;
    }

    public static function sockNotFound($response, $actionName)
    {
        $return_data = [
            'code' => '404',
            'msg' => "action {$actionName} not found",
            'content' => [],
        ];

        return $response->write(json_encode($return_data));
    }

    public static function succSend($response, $data = [], $msg = 'Success')
    {
        $return_data = [
            'code' => '200',
            'msg' => $msg,
            'content' => $data,
        ];

        return $response->write(json_encode($return_data));
    }

    public static function errSend($response, $data = [], $msg = 'error')
    {
        $return_data = [
            'code' => '500',
            'msg' => $msg,
            'content' => $data,
        ];

        return $response->write(json_encode($return_data));
    }

    public static function getStatus ($val) {
//        echo "++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
//        var_dump($val['bet_result']).PHP_EOL;
//        echo "++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
        switch ($val['bet_result']) {
            case 0:
                return '未开奖';
                break;
            case 1:
                return "中({$val["bet_res_amount"]})";
                break;
            case 2:
                return '未中奖';
                break;
            default:
                return '未开奖';
                break;

        }

    }


    public static function getUnit($type){
//        echo "++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
//        var_dump($type).PHP_EOL;
//        echo "++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
        switch ($type) {
            case 'big':
                return '大';
                break;
            case 'small':
                return '小';
                break;
            case 'double':
                return '双';
                break;
            case 'single':
                return '单';
                break;
        }
    }


    public static function formatNumber($number, $type = 'wan')
    {
        $unit = '';
        if($number > 10000){
            $number = $number / 10000;
            $unit = '万';
        }
        return $number . $unit;
    }

    public static function handle_number($string_number = '')
    {

        $is_double = function ($val) {

        };
        $result = [];
        if (strpos($string_number, ',') != FALSE) {
            $number_arr = explode(',', $string_number);
            $result['wan'] = self::number_define($number_arr[0]);
            $result['qian'] = self::number_define($number_arr[1]);
            $result['bai'] = self::number_define($number_arr[2]);
            $result['shi'] = self::number_define($number_arr[3]);
            $result['ge'] = self::number_define($number_arr[4]);
        }
        return $result;


    }

    public static function number_define($val)
    {
        $type_arr[0] = 'single';
        $type_arr_text[0] = '单';
        $type_arr[1] = 'big';
        $type_arr_text[1] = '大';
        if ($val % 2 == 0) {
            $type_arr[0] = 'double';
            $type_arr_text[0] = '双';

        }
        if ($val >= 0 && $val < 5) {
            $type_arr[1] = 'small';
            $type_arr_text[1] = '小';
        }
        return ['value' => $val, 'type' => $type_arr, 'text' => $type_arr_text,'type_l'=> implode(',',$type_arr),'type_arr_l' => implode('',$type_arr_text)];

    }


    public static function cartesian($arr, $str = array())
    {
        //去除第一个元素
        $first = array_shift($arr);

        //判断是否是第一次进行拼接
        if (count($str) > 1) {
            foreach ($str as $k => $val) {
                foreach ($first as $key => $value) {
                    //最终实现的格式 1,3,76
                    //可根据具体需求进行变更
                    $str2[] = $val . ',' . $value;
                }
            }
        } else {
            foreach ($first as $key => $value) {
                //最终实现的格式 1,3,76
                //可根据具体需求进行变更
                $str2[] = $value;
            }
        }

        //递归进行拼接
        if (count($arr) > 0) {
            $str2 = self::cartesian($arr, $str2);
        }
        //返回最终笛卡尔积
        return $str2;
    }

    public static function CartesianProduct($sets){

        // 保存结果
        $result = array();

        // 循环遍历集合数据
        for($i=0,$count=count($sets); $i<$count-1; $i++){

            // 初始化
            if($i==0){
                $result = $sets[$i];
            }

            // 保存临时数据
            $tmp = array();

            // 结果与下一个集合计算笛卡尔积
            foreach($result as $res){
                foreach($sets[$i+1] as $set){
                    $tmp[] = $res.','.$set;
                }
            }

            // 将笛卡尔积写入结果
            $result = $tmp;

        }

        return $result;

    }


    public static function format_add($str){
        return sprintf('%03s', $str);
    }

}
